﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EvotingSystemProject.Models;

namespace EvotingSystemProject.Controllers
{ 
    public class representitveController : Controller
    {
        private EvotingEntities db = new EvotingEntities();

        //
        // GET: /representitve/

        public ViewResult Index()
        {
            var representitves = db.Representitves.Include(r => r.District).Include(r => r.User);
            return View(representitves.ToList());
        }

        //
        // GET: /representitve/Details/5

        public ViewResult Details(int id)
        {
            Representitve representitve = db.Representitves.Find(id);
            return View(representitve);
        }

        //
        // GET: /representitve/Create

        public ActionResult Create()
        {
            ViewBag.DistrictID = new SelectList(db.Districts, "id", "name");
            ViewBag.userID = new SelectList(db.Users, "id", "name");
            return View();
        } 

        //
        // POST: /representitve/Create

        [HttpPost]
        public ActionResult Create(Representitve representitve)
        {
            if (ModelState.IsValid)
            {
                db.Representitves.Add(representitve);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.DistrictID = new SelectList(db.Districts, "id", "name", representitve.DistrictID);
            ViewBag.userID = new SelectList(db.Users, "id", "name", representitve.userID);
            return View(representitve);
        }
        
        //
        // GET: /representitve/Edit/5
 
        public ActionResult Edit(int id)
        {
            Representitve representitve = db.Representitves.Find(id);
            ViewBag.DistrictID = new SelectList(db.Districts, "id", "name", representitve.DistrictID);
            ViewBag.userID = new SelectList(db.Users, "id", "name", representitve.userID);
            return View(representitve);
        }

        //
        // POST: /representitve/Edit/5

        [HttpPost]
        public ActionResult Edit(Representitve representitve)
        {
            if (ModelState.IsValid)
            {
                db.Entry(representitve).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DistrictID = new SelectList(db.Districts, "id", "name", representitve.DistrictID);
            ViewBag.userID = new SelectList(db.Users, "id", "name", representitve.userID);
            return View(representitve);
        }

        //
        // GET: /representitve/Delete/5
 
        public ActionResult Delete(int id)
        {
            Representitve representitve = db.Representitves.Find(id);
            return View(representitve);
        }

        //
        // POST: /representitve/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Representitve representitve = db.Representitves.Find(id);
            db.Representitves.Remove(representitve);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}